package com.arraymasterx.controller;

import com.arraymasterx.model.request.SearchRequest;
import com.arraymasterx.model.response.SearchResponse;
import com.arraymasterx.service.SearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/search")
@Tag(name = "Search Algorithms", description = "Operations related to search algorithms")
public class SearchController {

    private final SearchService searchService;

    @Autowired
    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    @PostMapping("/binary")
    @Operation(summary = "Perform binary search on a sorted array", responses = {
            @ApiResponse(responseCode = "200", description = "Successful operation", content = @Content(schema = @Schema(implementation = SearchResponse.class))),
            @ApiResponse(responseCode = "400", description = "Invalid input") })
    public ResponseEntity<SearchResponse> binarySearch(@RequestBody SearchRequest request) {
        SearchResponse response = searchService.binarySearch(request);
        return ResponseEntity.ok(response);
    }
}